


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > AnimatedSprite</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/idea.min.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.min.js"></script>
  <script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">nl.tudelft.jpacman.sprite</a>
</div>

<h1>Coverage Summary for Class: AnimatedSprite (nl.tudelft.jpacman.sprite)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">AnimatedSprite</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    63.6%
  </span>
  <span class="absValue">
    (7/11)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    70.5%
  </span>
  <span class="absValue">
    (31/44)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<code class="sourceCode" id="sourceCode">&nbsp;package nl.tudelft.jpacman.sprite;
&nbsp;
&nbsp;import java.awt.Graphics;
&nbsp;
&nbsp;/**
&nbsp; * Animated sprite, renders the frame depending on the time of requesting the
&nbsp; * draw.
&nbsp; *
&nbsp; * @author Jeroen Roosen 
&nbsp; */
<b class="fc">&nbsp;public class AnimatedSprite implements Sprite {</b>
&nbsp;
&nbsp;    /**
&nbsp;     * Static empty sprite to serve as the end of a non-looping sprite.
&nbsp;     */
<b class="fc">&nbsp;    private static final Sprite END_OF_LOOP = new EmptySprite();</b>
&nbsp;
&nbsp;    /**
&nbsp;     * The animation itself, in frames.
&nbsp;     */
&nbsp;    private final Sprite[] animationFrames;
&nbsp;
&nbsp;    /**
&nbsp;     * The delay between frames.
&nbsp;     */
&nbsp;    private final int animationDelay;
&nbsp;
&nbsp;    /**
&nbsp;     * Whether is animation should be looping or not.
&nbsp;     */
&nbsp;    private final boolean looping;
&nbsp;
&nbsp;    /**
&nbsp;     * The index of the current frame.
&nbsp;     */
&nbsp;    private int current;
&nbsp;
&nbsp;    /**
&nbsp;     * Whether this sprite is currently animating or not.
&nbsp;     */
&nbsp;    private boolean animating;
&nbsp;
&nbsp;    /**
&nbsp;     * The {@link System#currentTimeMillis()} stamp of the last update.
&nbsp;     */
&nbsp;    private long lastUpdate;
&nbsp;
&nbsp;    /**
&nbsp;     * Creates a new animating sprite that will change frames every interval. By
&nbsp;     * default the sprite is not animating.
&nbsp;     *
&nbsp;     * @param frames
&nbsp;     *            The frames of this animation.
&nbsp;     * @param delay
&nbsp;     *            The delay between frames.
&nbsp;     * @param loop
&nbsp;     *            Whether or not this sprite should be looping.
&nbsp;     */
&nbsp;    public AnimatedSprite(Sprite[] frames, int delay, boolean loop) {
<b class="fc">&nbsp;        this(frames, delay, loop, false);</b>
<b class="fc">&nbsp;    }</b>
&nbsp;
&nbsp;    /**
&nbsp;     * Creates a new animating sprite that will change frames every interval.
&nbsp;     *
&nbsp;     * @param frames
&nbsp;     *            The frames of this animation.
&nbsp;     * @param delay
&nbsp;     *            The delay between frames.
&nbsp;     * @param loop
&nbsp;     *            Whether or not this sprite should be looping.
&nbsp;     * @param isAnimating
&nbsp;     *            Whether or not this sprite is animating from the start.
&nbsp;     */
<b class="fc">&nbsp;    public AnimatedSprite(Sprite[] frames, int delay, boolean loop, boolean isAnimating) {</b>
<b class="fc">&nbsp;        assert frames.length &gt; 0;</b>
&nbsp;
<b class="fc">&nbsp;        this.animationFrames = frames.clone();</b>
<b class="fc">&nbsp;        this.animationDelay = delay;</b>
<b class="fc">&nbsp;        this.looping = loop;</b>
<b class="fc">&nbsp;        this.animating = isAnimating;</b>
&nbsp;
<b class="fc">&nbsp;        this.current = 0;</b>
<b class="fc">&nbsp;        this.lastUpdate = System.currentTimeMillis();</b>
<b class="fc">&nbsp;    }</b>
&nbsp;
&nbsp;    /**
&nbsp;     * @return The frame of the current index.
&nbsp;     */
&nbsp;    private Sprite currentSprite() {
<b class="fc">&nbsp;        Sprite result = END_OF_LOOP;</b>
<b class="fc">&nbsp;        if (current &lt; animationFrames.length) {</b>
<b class="fc">&nbsp;            result = animationFrames[current];</b>
&nbsp;        }
<b class="fc">&nbsp;        assert result != null;</b>
<b class="fc">&nbsp;        return result;</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * Starts or stops the animation of this sprite.
&nbsp;     *
&nbsp;     * @param isAnimating
&nbsp;     *            &lt;code&gt;true&lt;/code&gt; to animate this sprite or &lt;code&gt;false&lt;/code&gt;
&nbsp;     *            to stop animating this sprite.
&nbsp;     */
&nbsp;    public void setAnimating(boolean isAnimating) {
<b class="fc">&nbsp;        this.animating = isAnimating;</b>
<b class="fc">&nbsp;    }</b>
&nbsp;
&nbsp;    /**
&nbsp;     * (Re)starts the current animation.
&nbsp;     */
&nbsp;    public void restart() {
<b class="nc">&nbsp;        this.current = 0;</b>
<b class="nc">&nbsp;        this.lastUpdate = System.currentTimeMillis();</b>
<b class="nc">&nbsp;        setAnimating(true);</b>
<b class="nc">&nbsp;    }</b>
&nbsp;
&nbsp;    @Override
&nbsp;    public void draw(Graphics graphics, int x, int y, int width, int height) {
<b class="fc">&nbsp;        update();</b>
<b class="fc">&nbsp;        currentSprite().draw(graphics, x, y, width, height);</b>
<b class="fc">&nbsp;    }</b>
&nbsp;
&nbsp;    @Override
&nbsp;    public Sprite split(int x, int y, int width, int height) {
<b class="nc">&nbsp;        update();</b>
<b class="nc">&nbsp;        return currentSprite().split(x, y, width, height);</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * Updates the current frame index depending on the current system time.
&nbsp;     */
&nbsp;    private void update() {
<b class="fc">&nbsp;        long now = System.currentTimeMillis();</b>
<b class="fc">&nbsp;        if (animating) {</b>
<b class="fc">&nbsp;            while (lastUpdate &lt; now) {</b>
<b class="fc">&nbsp;                lastUpdate += animationDelay;</b>
<b class="fc">&nbsp;                current++;</b>
<b class="fc">&nbsp;                if (looping) {</b>
<b class="fc">&nbsp;                    current %= animationFrames.length;</b>
<b class="nc">&nbsp;                } else if (current == animationFrames.length) {</b>
<b class="nc">&nbsp;                    animating = false;</b>
&nbsp;                }
&nbsp;            }
&nbsp;        } else {
<b class="nc">&nbsp;            lastUpdate = now;</b>
&nbsp;        }
<b class="fc">&nbsp;    }</b>
&nbsp;
&nbsp;    @Override
&nbsp;    public int getWidth() {
<b class="nc">&nbsp;        assert currentSprite() != null;</b>
<b class="nc">&nbsp;        return currentSprite().getWidth();</b>
&nbsp;    }
&nbsp;
&nbsp;    @Override
&nbsp;    public int getHeight() {
<b class="nc">&nbsp;        assert currentSprite() != null;</b>
<b class="nc">&nbsp;        return currentSprite().getHeight();</b>
&nbsp;    }
&nbsp;
&nbsp;}
</code>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
      hljs.highlightAll()
      hljs.initLineNumbersOnLoad();
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2023-04-11 19:46</div>
</div>
</body>
</html>
